<template>
  <div>
    <el-form ref="summaryViewForm" :model="formItem" label-width="120px" size="mini">
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="服务编号" prop="registerCode">
            {{ registerCode }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="巡检厅局" prop="unitName">
            {{ unitName }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="执行人" prop="reportPersonName">
            {{ formItem.reportPersonName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            {{ formItem.startTime }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            {{ formItem.endTime }}
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="服务总结" prop="record">
            {{ formItem.record }}
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件">
            <el-button v-for="tag in formItem.files" :key="tag.name" size="mini" type="text" @click.native="downloadFile(tag)">{{ tag.name }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { summaryDetail, summaryDownload } from '@neu/dcom-ui/api/insuranceRegisterTask'
import { getToken } from '@neu/biz-core/utils/auth'
import { formatDate } from '@neu/biz-core/utils/utils'
// 定义一个表单原始项
const formItem = {
  code: '',
  reportPersonCode: '',
  reportPersonName: '',
  unitCode: '',
  startTime: '',
  endTime: '',
  status: '',
  record: '',
  files: []
}
export default {
  name: 'SummaryViewForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    },
    registerCode: {
      required: false,
      type: String,
      default: ''
    },
    unitName: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler() {
        if (this.id) {
          this.fetch()
        }
      },
      immediate: true
    }
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        summaryDetail(this.id).then(response => {
          console.log(response.data)
          this.formItem = {
            ...response.data
          }
          this.formItem.startTime = formatDate(this.formItem.startTime, 'YYYY-MM-DD')
          this.formItem.endTime = formatDate(this.formItem.endTime, 'YYYY-MM-DD')
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    /** 下载附件 */
    downloadFile(tag) {
      summaryDownload(tag.fsId).then(res => {
        const content = res
        const blob = new Blob([content])
        const fileName = tag.name
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    }
  }
}
</script>

